package com.viego.financialrecommendsystem.controller;

import com.viego.financialrecommendsystem.common.AjaxResult;
import com.viego.financialrecommendsystem.entity.Article;
import com.viego.financialrecommendsystem.entity.Menu;
import com.viego.financialrecommendsystem.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RequestMapping("/article")
@RestController
public class ArticleController {

    @Resource
    IArticleService articleService;

    @GetMapping("/query")
    public AjaxResult query(@RequestParam("articleId") String  articleId){
        log.info("已进入 ArticleController ");
        Long id = Long.parseLong(articleId);
        log.info("要查询的文章的 id = {}",articleId);
        Article article = articleService.queryById(id);
        log.info("文章查询成功~~");
        return AjaxResult.success("文章查询成功",article);
    }

    @GetMapping("/menu")
    public AjaxResult menu(){
        List<Menu> menu = articleService.menu();
        return AjaxResult.success("菜单查询成功",menu);
    }

    @GetMapping("/collect")
    public AjaxResult collect(@RequestParam("articleId") String articleId){
        Integer number = articleService.collect(Long.parseLong(articleId));
        return AjaxResult.success("查询成功",number);
    }

    @GetMapping("/addCollect")
    public AjaxResult addCollect(@RequestParam("userId") String userId,
                                 @RequestParam("articleId") String articleId){
        //先检查一下是否已经被收藏了，
        boolean f =articleService.isCollected(Long.parseLong(userId),Long.parseLong(articleId));
        if (f)
            return AjaxResult.error("无需重复收藏");
        boolean flag =  articleService.addCollect(userId,articleId);
        if (flag){
            return AjaxResult.success("收藏成功");
        }else{
            return  AjaxResult.error("添加失败");
        }
    }

    @GetMapping("/Myarticle")
    public AjaxResult MyArticle(@RequestParam("userId")String userId){
        List<Article> list = articleService.article(Long.parseLong(userId));
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody Article article){
        boolean flag = articleService.save(article);
        if (flag){
            return AjaxResult.success("发表成功");
        }else{
            return AjaxResult.error("发表失败");
        }
    }

    @GetMapping("/type")
    public AjaxResult Type(){
        List<String> types = articleService.type();
        return AjaxResult.success("查询成功",types);
    }


    @GetMapping("/removeCollect")
    public AjaxResult removeCollect(@RequestParam("userId")String userId,
                                    @RequestParam("articleId")String articleId){
        boolean flag = articleService.removeCollect(Long.parseLong(userId),Long.parseLong(articleId));
        if (flag){
            return AjaxResult.success("操作成功");
        }else{
            return AjaxResult.error("操作失败");
        }
    }





}
